package com.controller;

import com.base.*;
import com.bean.*;
import com.bean.anno.LogAnno;
import com.bean.anno.VisitAnno;
import com.bean.system.MenuInfo;
import com.bean.wechat.WechatContent;
import com.github.pagehelper.PageHelper;
import com.mapper.mysql.FrontMapper;
import com.mapper.mysql.WebsiteMessageMapper;
import com.mapper.repository.mysql.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("front")
public class FrontController extends BaseController {


    @Autowired
    private FrontMapper frontMapper;

    @LogAnno(apiName = "前端获取大类列表")
    @GetMapping("categorys")
    public AjaxResult categorys(){
        List<SelectInfo> categorys = frontMapper.categorys();
        return success("获取成功",categorys);

    }

    @LogAnno(apiName = "前端获取前十的标签，方便快捷查询")
    @PostMapping("types")
    public AjaxResult types(@RequestBody SelectInfo selectInfo){
        List<SelectInfo> types = frontMapper.types(selectInfo.getValue());
        return success("获取成功",types);

    }

    @LogAnno(apiName = "前端获取前十的标签")
    @GetMapping("tags")
    public AjaxResult tags(){
        List<SelectInfo> tags = frontMapper.tags();
        return success("获取成功",tags);

    }

    @Autowired
    private WechatContentRepository wechatContentRepository;

    @Autowired
    private WebsiteTagsRepository websiteTagsRepository;

    @VisitAnno(apiName = "前端获取网址信息 listWebsite",flag = true)
    @LogAnno(apiName = "前端获取网址")
    @PostMapping("listWebsite")
    public AjaxResult listWebsite(@RequestBody WebsiteContent websiteContent){
        if(websiteContent.getWid()!=null&&!websiteContent.getWid().equals("")){
            WechatContent wechatContent = wechatContentRepository.getById(websiteContent.getWid());
            websiteContent.setDescription(wechatContent.getContent());
        }

        //保存标签
        if(websiteContent.getDescription()!=null&&!websiteContent.getDescription().equals("")){
            WebsiteTags websiteTags = websiteTagsRepository.getWebsiteTagsByTag(websiteContent.getDescription());
            if(websiteTags!=null){
                websiteTags.setNum(websiteTags.getNum()+1);
            }else{
                websiteTags = new WebsiteTags();
                websiteTags.setId(UUID.randomUUID().toString());
                websiteTags.setNum(1);
                websiteTags.setCreatetime(LocalDateTime.now());
                websiteTags.setTag(websiteContent.getDescription());
            }
            websiteTagsRepository.save(websiteTags);
        }

        List<WebsiteContent> websites = frontMapper.listWebsites(websiteContent);
        return success("获取成功",websites);

    }



    @Autowired
    private WebsiteVisitorUrlRepository websiteVisitorUrlRepository;

    @PostMapping("visitUrl")
    public void visitUrl(@RequestBody WebsiteVisitorUrl websiteVisitorUrl){

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String addr = request.getRemoteAddr();
        websiteVisitorUrl.setAddr(addr);
        websiteVisitorUrl.setCreatetime(LocalDateTime.now());
        websiteVisitorUrl.setId(UUID.randomUUID().toString());

        websiteVisitorUrlRepository.save(websiteVisitorUrl);
    }


    @Autowired
    private WebsiteMessageMapper websiteMessageMapper;

    @Autowired
    private WebsiteMessageRepository websiteMessageRepository;

    @LogAnno(apiName = "获取留言")
    @PostMapping("listMessage")
    public AjaxResult list(@RequestBody AjaxRequest<WebsiteMessage> request){
        WebsiteMessage message = request.getT();
        MetaInfo metaInfo = request.getMetaInfo();

        PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        List<WebsiteMessage> list = websiteMessageMapper.select();

        return success("查询成功",list,metaInfo);
    }

    @LogAnno(apiName = "添加留言")
    @PostMapping("addMessage")
    public AjaxResult add(@RequestBody WebsiteMessage message){
        String id = UUID.randomUUID().toString();
        message.setId(id);
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String addr = request.getRemoteAddr();
        message.setAddr(addr);
        message.setCreatetime(LocalDateTime.now());
        websiteMessageRepository.save(message);
        return success("新增成功");
    }


//    @Autowired
//    private WechatContentRepository wechatContentRepository;

    @Autowired
    private LogintokenSourceRepository logintokenSourceRepository;

    @Autowired
    private LogintokenContentRepository logintokenContentRepository;

    @LogAnno(apiName = "获取微信公众号提问的内容")
    @PostMapping("getDescription")
    public AjaxResult getDescription(@RequestBody WebsiteContent websiteContent){

        String wid  = websiteContent.getWid();
        WechatContent wechatContent = wechatContentRepository.getById(wid);
        if(wechatContent.getType()!=null&&wechatContent.getType().equals(Constants.WechatLogintoken.getValue())){
            LocalDateTime endtime = wechatContent.getEndtime();
            if(endtime.isBefore(LocalDateTime.now())){
                return error("内容已过期");
            }else{
                LogintokenSource logintokenSource = logintokenSourceRepository.findLogintokenSourceById(wechatContent.getOther());
                LogintokenContent logintokenContent = logintokenContentRepository.findLogintokenContentBySid(wechatContent.getOther());
                Map map = new HashMap();
                map.put("logintokenSource",logintokenSource);
                map.put("logintokenContent",logintokenContent);
                return success("获取成功",map);
            }
        }else{
            return success("获取成功",wechatContent);
        }


    }
}
